﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Wpf_Examples.Views;
using static UserControlLib.Enums.PopWindowEnum;

namespace Wpf_Examples.ViewModels
{
    public class MainViewModel : ObservableObject
    {
        private string dateTimeStr;
        public string DateTimeStr
        {
            get { return dateTimeStr; }
            set { SetProperty(ref dateTimeStr, value); }
        }

        public RelayCommand<string> ButtonClickCmd { get; set; }
        public object OtherWay1 { get; private set; }
        public object OtherWay2 { get; private set; }

        public MainViewModel()
        {
            ButtonClickCmd = new RelayCommand<string>(FunMenu);

            //创建定时器
            CreateTimer();
        }

        private void CreateTimer()
        {
            #region 每秒定时器服务
            DispatcherTimer cpuTimer = new DispatcherTimer
            {
                Interval = new TimeSpan(0, 0, 0, 1, 0)
            };
            cpuTimer.Tick += DispatcherTimer_Tick;
            cpuTimer.Start();
            #endregion
        }

        private int number = 12345;
        private void DispatcherTimer_Tick(object sender, EventArgs e)
        {
            //系统时间
            DateTimeStr = DateTime.Now.ToString("HHmmss");

            // 更新数字
            number++;
            DateTimeStr = number.ToString("D9"); // 设置为9位数，不足的前面补0
        }

        private void FunMenu(string obj)
        {
            var mainWindowInstance = App.Current.MainWindow; // 获取主窗口实例
            bool? result;//定义窗体点击的结果选项
            switch (obj)
            {
                case "图片按钮":
                    PopWindow(new ImageButtonWindow());
                    break;
                case "LED效果灯":
                    PopWindow(new LEDStatusWindow());
                    break;
                case "动态数字卡":
                    PopWindow(new DataCardWindow());
                    break;
                case "自定义GroupBox边框":
                    PopWindow(new GroubBoxWindow());
                    break;
                case "自定义雷达图":
                    PopWindow(new RadarWindow());
                    break;
                case "分页控件":
                    PopWindow(new PageBarWindow());
                    break;
                case "开关按钮":
                    PopWindow(new ToggleButtonWindow());
                    break;
                case "霓虹灯字特效":
                    PopWindow(new NeonTextWindow());
                    break;
                case "环形进度条":
                    PopWindow(new CircularProgressBar());
                    break;
                case "3D翻折计数":
                    PopWindow(new NumberFoldingCard());
                    break;
                case "警告弹窗":

                    var warningNotification = new UserControlLib.SMessageBox()
                    {
                        Icon = IconType.Warning,
                        ButtonType = ButtonType.OkCancel,
                        Title = "警告",
                        Content = "这是一条警告信息",
                    };
                    warningNotification.Owner = mainWindowInstance; // 设置父窗口
                    result = warningNotification.ShowDialog();
                    MessageBox.Show($"点击窗体的结果是{result}", "提示", MessageBoxButton.OK);
                    break;
                case "错误弹窗":
                    var error = new UserControlLib.SMessageBox()
                    {
                        Icon = IconType.Error,
                        ButtonType = ButtonType.Ok,
                        Title = "错误",
                        Content = "这是一条错误信息",
                    };
                    error.Owner = mainWindowInstance; // 设置父窗口
                    result = error.ShowDialog();
                    MessageBox.Show($"点击窗体的结果是{result}", "提示", MessageBoxButton.OK);
                    break;
                case "提示弹窗":
                    var info = new UserControlLib.SMessageBox()
                    {
                        Icon = IconType.Info,
                        ButtonType = ButtonType.Ok,
                        Title = "错误",
                        Content = "这是一条错误信息",
                    };
                    info.Owner = mainWindowInstance; // 设置父窗口
                    result = info.ShowDialog();
                    MessageBox.Show($"点击窗体的结果是{result}", "提示", MessageBoxButton.OK);
                    break;
                case "下拉菜单":
                    PopWindow(new DropDownMenuWindow());
                    break;
                case "自定义按钮":
                    PopWindow(new ButtonWindow());
                    break;

                case "雷达辐射效果":
                    PopWindow(new RadarEffactWindow());
                    break;
                case "侧边弹窗栏":
                    PopWindow(new PopPanelWindow());
                    break;
                case "阴影效果":
                    PopWindow(new ShadowEffectWindow());
                    break;
                case "粒子效果":
                    PopWindow(new ParticleWindow());
                    break;
                case "3D粒子波浪效果":
                    PopWindow(new ParticleWaveEffect());
                    break;
                case "3D粒子方块波浪墙效果":
                    PopWindow(new ParticleCubeWaveWindow());
                    break;
                case "3D粒子球波浪墙效果":
                    PopWindow(new ParticleBallWaveWindow());
                    break;
                case "Y轴图像":
                    PopWindow(new MultiYChartWindow());
                    break;
                case "PDF加载预览":
                    PopWindow(new LoadPDFAndWordWindow());
                    break;
                case "日志控件":
                    PopWindow(new LogPanelWindow());
                    break;
                case "加载控件":
                    GetProduct();
                    break;
                case "键盘辅助":
                    PopWindow(new KeyboardDemo());
                    break;
                case "仪表盘":
                    PopWindow(new GaugeDemoWindow());
                    break;
                case "数字滚动":
                    PopWindow(new RollingNumWindow());
                    break;
                case "文本控件":
                    PopWindow(new TextBoxWindow());
                    break;
                case "弧形进度条":
                    PopWindow(new ArcProgressBarWindow());
                    break;
                case "汉堡菜单":
                    PopWindow(new ArcProgressBarWindow());
                    break;
                case "获取文本Path":
                    PopWindow(new TxtPathWindow());
                    break;
                case "多语言切换":
                    PopWindow(new LanguageChangeWindow());
                    break;
                case "ListBox侧边菜单":
                    PopWindow(new ListBoxMenuWindow());
                    break;

                    
                case "流水边框":
                    PopWindow(new FlowWaterBorderWindow());
                    break;
                    
            }
        }

        private async void GetProduct()
        {
            UserControlLib.LoadIngWin loadIngWin = new UserControlLib.LoadIngWin();
            loadIngWin.WindowStartupLocation= WindowStartupLocation.CenterScreen;
            //可以通过属性来设置弹窗加载窗体的样式，不设置采用默认值
            loadIngWin.Width = 160;
            loadIngWin.Height = 160;
            loadIngWin.TextBlockText = "Loading...";
            loadIngWin.Show();
            try
            {
                var pList = await Task.Run(() => GetApiData());
                if (pList != null && pList.Count > 0)
                {
                    // 使用 Dispatcher.Invoke 确保在 UI 线程上更新日志
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        //这里用来更新UI页面日志
                    });

                    //其他所有耗时操作均放在异步线程中实现，这样主要用来防止方法耗时卡UI,导致动画卡顿不流畅
                    await Task.Run(() =>OtherWay1);
                    await Task.Run(() => OtherWay2);
                }
                else
                {
                    // 使用 Dispatcher.Invoke 确保在 UI 线程上更新日志
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        //这里用来更新UI页面日志
                    });
                }
            }
            catch (Exception ex)
            {
                //这里用来更新UI页面日志
            }

            // 延迟10秒后关闭窗口，但不阻塞UI线程 模拟，实际使用不需要这句话
            await Task.Delay(10000);
         
            loadIngWin.Close();
        }

        private List<string> GetApiData()
        {
            return null;
        }

        private void PopWindow(Window window)
        {
            var mainWindowInstance = App.Current.MainWindow; // 获取主窗口实例
            window.Owner = mainWindowInstance;
            window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            window.ShowDialog();
        }
    }
}
